// pages/staff/myyejievery.js
const app = getApp()
var CONFIG = require("../../config/config.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        target: 0,  //0本日 1本周 2本月
        type: 1,     //1CRM已确认   2CRM未确认    3网课已确认  4网课未确认 
        page:0,
        lists:[],
        title:'',
        amount:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var consultant_id = app.globalData.admin_id;
        var target = options.tagge;
        var type = options.type;
        that.setData({
            target: target,
            type: type,
        })
        // if()
        if (type == 1 || type == '1') {
            wx.setNavigationBarTitle({
                title: 'CRM业绩（已确认）'
            })
            that.setData({
                title: 'CRM业绩（已确认）'
            })
            that.yejiLoadMore(consultant_id, target,0)
        } else if(type == 2 || type == '2') {
            wx.setNavigationBarTitle({
                title: 'CRM业绩（未确认）'
            })
            that.setData({
                title: 'CRM业绩（未确认）'
            })
            that.yejiLoadMore(consultant_id, target, 0)
        } else if (type == 3 || type == '3') {
            wx.setNavigationBarTitle({
                title: '网课业绩（已确认）'
            })
            that.setData({
                title: '网课业绩（已确认）'
            })
            that.yejiInitWkYqr(consultant_id, target, 0)
        }else{
            wx.setNavigationBarTitle({
                title: '网课业绩（未确认）'
            })
            that.setData({
                title: '网课业绩（未确认）'
            })
            that.yejiInitWkWqr(consultant_id, target, 0)
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        // console.log(8989);
        wx.showLoading({
            title: '加载中~',
        })
        var consultant_id = app.globalData.admin_id;
        var target = that.data.target;
        var page = that.data.page;
        var type = that.data.type;
        if (type == 4 || type == '4') {    // 网课未确认
            that.yejiInitWkWqr(consultant_id, target, page);
        } else if (type == 3 || type == '3') {    // 网课已确认
            that.yejiInitWkYqr(consultant_id, target, page);
        } else {
            that.yejiLoadMore(consultant_id, target, page);
        }
    },

    // 网课已确认
    yejiInitWkYqr(consultant_id, target, page) {
        var that = this;
        if (page == 0 || page == '0') {
            var data = {
                consultant_id: consultant_id,
                target: target,
            }
        } else {
            var data = {
                consultant_id: consultant_id,
                target: target,
                page: page
            }
        }
        wx.request({
            url: CONFIG.API_HOST + 'crm/static/yejiInitWkYqr',
            data: data,
            header: {
                'ContentType': 'application/json'
            },
            success: function (res) {
                // console.log(res);
                if (res.data.code == 1 || res.data.code == '1') {
                    var newslist = res.data.list;
                    // console.log(newslist)
                    // console.log(that.data.nowifilists)
                    var lists = that.data.lists.concat(newslist);
                    // console.log(lists);
                    // console.log(res.data.amount[0])
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        pages: res.data.pages,
                        page: res.data.page,
                        lists: lists,
                        amount: res.data.amount[0]
                    })
                    // console.log(that.data.page);
                } else {
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        pages: res.data.pages,
                        page: res.data.page,
                        lists: that.data.lists,
                        amount: res.data.amount[0]
                    })
                }
                wx.hideLoading();
            },
            fail: function (res) {
                wx.hideLoading();
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
    },

    // 网课未确认
    yejiInitWkWqr(consultant_id, target, page) {
        var that = this;
        var page = that.data.page;
        if (page == 0 || page == '0') {
            var data = {
                consultant_id: consultant_id,
                target: target,
            }
        } else {
            var data = {
                consultant_id: consultant_id,
                target: target,
                page: page
            }
        }
        wx.request({
            url: CONFIG.API_HOST + 'crm/static/yejiInitWkWqr',
            data: data,
            header: {
                'ContentType': 'application/json'
            },
            success: function (res) {
                // console.log(res);
                if (res.data.code == 1 || res.data.code == '1') {
                    var newslist = res.data.list;
                    // console.log(newslist)
                    // console.log(that.data.nowifilists)
                    var lists = that.data.lists.concat(newslist);
                    // console.log(lists);
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        pages: res.data.pages,
                        page: res.data.page,
                        lists: lists,
                        amount: res.data.amount[0]
                    })
                    // console.log(that.data.wifilistsamount)
                } else {
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        pages: res.data.pages,
                        page: res.data.page,
                        amount: res.data.amount[0]
                    })
                }
                wx.hideLoading();
            },
            fail: function (res) {
                wx.hideLoading();
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
    },



    yejiLoadMore: function (consultant_id, target, page) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        var type = that.data.type;
        if (page == 0 || page == '0') {
            var data = {
                consultant_id: consultant_id,
                target: target,
            }
        } else {
            var data = {
                consultant_id: consultant_id,
                target: target,
                page: page
            }
        }
        wx.request({
            url: CONFIG.API_HOST + 'crm/static/yejiInit',
            data: data,
            header: {
                'ContentType': 'application/json'
            },
            success: function (res) {
                // console.log(res);
                if (res.data.code == 1 || res.data.code == '1') {
                    var newslist = res.data.list;
                    // var newslist = [];
                    // var type = that.data.type;
                    // for (var i = 0; i < newslist1.length;i++){
                    //     if (type == 1 || type == 1){
                    //         if (newslist1[i].status!=0){
                    //             newslist[newslist.length] = newslist1[i];
                    //         }
                    //     }else{
                    //         if (newslist1[i].status == 0) {
                    //             newslist[newslist.length] = newslist1[i];
                    //         } 
                    //     }
                    // }
                    var lists = that.data.lists.concat(newslist);
                    // console.log(lists)
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        pages: res.data.pages,
                        page: res.data.page,
                        lists: lists,
                        
                    })
                    if (type == 1 || type == 1) {
                        that.setData({
                            amount: res.data.amount[1]
                        })
                    } else if (type == 2 || type == 2){
                        that.setData({
                            amount: res.data.amount[0]
                        })
                    }
                } else {
                    that.setData({
                        code: res.data.code,
                        msg: res.data.msg,
                        pages: res.data.pages,
                        page: res.data.page,
                        lists: that.data.lists
                    })
                }
                if (type == 1 || type == 1) {
                    that.setData({
                        amount: res.data.amount[1]
                    })
                } else if (type == 2 || type == 2) {
                    that.setData({
                        amount: res.data.amount[0]
                    })
                }
                wx.hideLoading();
            },
            fail: function (res) {
                wx.hideLoading();
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },


})